// pages/yqwa/index.js

var WxParse = require('../common/wxParse/wxParse.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (param) {
    this.setData({
      code: wx.getStorageSync('dengl')
    })
  

    // 获取当前定位
    wx.getLocation({
      type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
      success: (res) => {
   
  console.log(res,"定位系统")
        this.getRegeo(res.longitude + ',' + res.latitude, true) 
        //res.longitude + ',' + res.latitude' 116.82209,38.3062'
        var s = res.longitude + ',' + res.latitude
          
        wx.setStorageSync('position', {
          location:s,
        })  
      }
    })
    console.log("param", param)

    // param.did=5
    // param.uid = 10537
    console.log(param, "扫码参数param")
    if (param.did) {
      var huodongid = param.did
      var yonghuid = param.uid

    } else {
      var str = param.scene
      var arr = []
      arr = str.split("_")
      var huodongid = arr[1]
      var yonghuid = arr[0]
      console.log(arr)
    }
    this.setData({
      huodongid: huodongid,
      yonghuid: yonghuid
    })
    // 商品id
    this.getMuDetail(huodongid)
  },
  getMuDetail:function(e){

    var _this = this
    // 请求会员等级
    var params = {
      inviteId :e
    }
    app.HttpService.qiingqiufuqwnb(params).then(res => {
      console.log(res, "数666据")
      WxParse.wxParse('article', 'html', res.data.data, this, 13) // 解析html代码 富文本显示
    });
  },


  baoy: function() {
// code==1是老用户
    if(this.data.code==1){
          let defaultCar = wx.getStorageSync('defaultCar')
        
    if (defaultCar) {
      wx.navigateTo({
        url: '/pages/product/package/index',
      })
    } else {
      wx.navigateTo({
        url: '/pages/car/firstMenu/index',
      })
    }  
    }else{
      var uid = this.data.yonghuid;
      var did = this.data.huodongid
      wx.navigateTo({
        url: '/pages/login/index?uid=' + uid + "&did=" + did,
      })


    }

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
  },

  getRegeo(lng, lat) {
     let l=  lng.split(",") 
    app.HttpService.sellerCountry({
      lng: l[0],
      lat: l[1]
    }).then(res => {
      let data = res.data
     
      let agent = {}
      agent.id = data.data.sellerId
      this.setData({
        agent
      });

      wx.setStorageSync('agent', agent);

      this.getDefault();


    })
  },
  // 邀请新人
  yaoxin:function(){

// 判断如果是老用户啊直接跳转
    if(this.data.code==1){
      wx.navigateTo({
        url: '/pages/fenxiangym/index',
      })
    }else{
      var yao = true
      var uid = this.data.yonghuid;
      var did = this.data.huodongid
      wx.navigateTo({
        url: '/pages/login/index?uid=' + uid + "&did=" + did + "&yao=" + yao,
      })
    }
  },
  getDefault() { // 获取默认车辆
    let userId = wx.getStorageSync('userId')
    let agent = wx.getStorageSync('agent')
    app.HttpService.getDefault({
      userId: userId
    }).then(res => {
      let data = res.data
      if (data.code == 200) {
        if (JSON.stringify(data.data.carInfo) != '{}') {
          let carInfo = data.data.carInfo
          let carModel = data.data.carModel
          let info = carInfo.brandName + ' ' + carInfo.factoryName + ' ' + carInfo.modelName
          let defaultCar = {
            carInfo: info,
            carId: carModel.id, // 默认车辆id
            carModelId: carModel.carModelId, // 车辆 车型三级id
            carFactoryId: carInfo.factoryId, // 车辆 车系二级id
            miles: carModel.miles,
            linkman: carModel.linkman,
            phone: carModel.phone,
            carCode: carModel.carAreaCode + carModel.carAreaNo,
            brandId: carInfo.brandId
          }
          wx.setStorageSync('defaultCar', defaultCar)

        }
      }
    })
  }






})